package com.company.cloud.web.controller;

import ch.qos.logback.core.joran.spi.ActionException;
import com.company.cloud.web.render.Render;
import com.jfinal.kit.StrKit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.Map;

public abstract class Controller {

    private HttpServletRequest request;
    private HttpServletResponse response;

    private String urlPara;
    private String[] urlParaArray;
    private String rawData;

    private Render render;


    void _init_(HttpServletRequest request, HttpServletResponse response, String urlPara) {
        this.request = request;
        this.response = response;
        this.urlPara = urlPara;
    }

    public Controller setAttr(String name, Object value) {
        request.setAttribute(name, value);
        return this;
    }

    public Controller removeAttr(String name) {
        request.removeAttribute(name);
        return this;
    }


    public Controller setAttrs(Map<String, Object> attrMap) {
        for (Map.Entry<String, Object> entry : attrMap.entrySet())
            request.setAttribute(entry.getKey(), entry.getValue());
        return this;
    }

    public String getPara(String name) {
        // return request.getParameter(name);
        String result = request.getParameter(name);
        return result != null && result.length() != 0 ? result : null;
    }

    public String getPara(String name, String defaultValue) {
        String result = request.getParameter(name);
        return result != null && !"".equals(result) ? result : defaultValue;
    }

    public Map<String, String[]> getParaMap() {
        return request.getParameterMap();
    }


    public Enumeration<String> getParaNames() {
        return request.getParameterNames();
    }

    public String[] getParaValues(String name) {
        return request.getParameterValues(name);
    }

    public Integer[] getParaValuesToInt(String name) {
        String[] values = request.getParameterValues(name);
        if (values == null || values.length == 0) {
            return null;
        }
        Integer[] result = new Integer[values.length];
        for (int i = 0; i < result.length; i++) {
            result[i] = StrKit.isBlank(values[i]) ? null : Integer.parseInt(values[i]);
        }
        return result;
    }

    public Long[] getParaValuesToLong(String name) {
        String[] values = request.getParameterValues(name);
        if (values == null || values.length == 0) {
            return null;
        }
        Long[] result = new Long[values.length];
        for (int i = 0; i < result.length; i++) {
            result[i] = StrKit.isBlank(values[i]) ? null : Long.parseLong(values[i]);
        }
        return result;
    }

    public Enumeration<String> getAttrNames() {
        return request.getAttributeNames();
    }

    public <T> T getAttr(String name) {
        return (T) request.getAttribute(name);
    }

    public <T> T getAttr(String name, T defaultValue) {
        T result = (T) request.getAttribute(name);
        return result != null ? result : defaultValue;
    }

    public String getAttrForStr(String name) {
        return (String) request.getAttribute(name);
    }

    public Integer getAttrForInt(String name) {
        return (Integer) request.getAttribute(name);
    }

    public String getHeader(String name) {
        return request.getHeader(name);
    }

//    private Integer toInt(String value, Integer defaultValue) {
//        try {
//            if (StrKit.isBlank(value))
//                return defaultValue;
//            value = value.trim();
//            if (value.startsWith("N") || value.startsWith("n"))
//                return -Integer.parseInt(value.substring(1));
//            return Integer.parseInt(value);
//        }
//        catch (Exception e) {
//            throw new ActionException(400, renderManager.getRenderFactory().getErrorRender(400),  "Can not parse the parameter \"" + value + "\" to Integer value.");
//        }
//    }


}
